<template>
  <div class="user-register-page">
    <div class="register-container">
      <div class="left-panel">
        <img src="https://img.freepik.com/free-photo/flat-lay-natural-self-care-products-composition_23-2148990019.jpg" alt="美妆商城" class="side-image">
        <div class="overlay">
          <div class="promo-text">
            <h2>加入美妆商城</h2>
            <p>成为会员，享受专属优惠与服务</p>
          </div>
        </div>
      </div>
      
      <div class="right-panel">
        <div class="logo-container">
          <img src="https://cdn-icons-png.flaticon.com/512/3588/3588244.png" alt="用户Logo" class="logo">
          <span class="logo-text">用户中心</span>
        </div>
        
        <div class="form-header">
          <h1>用户注册</h1>
          <p>填写以下信息创建您的账号</p>
        </div>
        
        <el-form
          ref="registerForm"
          :model="registerForm"
          :rules="rules"
          class="register-form"
          label-position="top"
        >
          <div class="form-row">
            <el-form-item label="用户名" prop="username">
              <el-input 
                v-model="registerForm.username" 
                placeholder="请输入用户名"
                :prefix-icon="User"
              />
            </el-form-item>
          </div>
          
          <div class="form-row">
            <el-form-item label="邮箱" prop="email">
              <el-input 
                v-model="registerForm.email" 
                placeholder="请输入邮箱"
                :prefix-icon="Message"
              />
            </el-form-item>
          </div>
          
          <div class="form-row">
            <el-form-item label="手机号" prop="phone">
              <el-input 
                v-model="registerForm.phone" 
                placeholder="请输入手机号"
                :prefix-icon="Phone"
              />
            </el-form-item>
          </div>
          
          <div class="form-row">
            <el-form-item label="密码" prop="password">
              <el-input 
                v-model="registerForm.password" 
                type="password"
                show-password 
                placeholder="请设置密码"
                :prefix-icon="Lock"
              />
            </el-form-item>
          </div>
          
          <div class="form-row">
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input 
                v-model="registerForm.confirmPassword" 
                type="password"
                show-password
                placeholder="请再次输入密码"
                :prefix-icon="Lock"
              />
            </el-form-item>
          </div>
          
          <div class="term-service">
            <el-checkbox v-model="agreeTerms">我已阅读并同意<a href="javascript:void(0)">服务条款</a>和<a href="javascript:void(0)">隐私政策</a></el-checkbox>
          </div>
          
          <el-button 
            type="primary" 
            :loading="loading" 
            @click="handleRegister" 
            class="submit-button"
            :disabled="!agreeTerms"
          >
            {{ loading ? '注册中...' : '注册账号' }}
          </el-button>
          
          <div class="form-footer">
            <p>已有账号? <router-link to="/login" class="login-link">立即登录</router-link></p>
            <p class="merchant-link"><router-link to="/merchant/register">商家注册入口</router-link></p>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useUserStore } from '../stores/user';
import { User, Lock, Message, Phone } from '@element-plus/icons-vue';

// 表单数据
const registerForm = ref({
  username: '',
  password: '',
  confirmPassword: '',
  email: '',
  phone: ''
});

const agreeTerms = ref(false);

// 密码一致性校验
const validatePass = (rule: any, value: string, callback: any) => {
  if (value === '') {
    callback(new Error('请再次输入密码'));
  } else if (value !== registerForm.value.password) {
    callback(new Error('两次输入密码不一致'));
  } else {
    callback();
  }
};

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: validatePass, trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  phone: [
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ]
};

// 获取全局状态和路由
const userStore = useUserStore();
const router = useRouter();

// 从store中获取loading状态
const loading = computed(() => userStore.loading);

// 注册处理
const handleRegister = async () => {
  if (!agreeTerms.value) {
    ElMessage.warning('请先阅读并同意服务条款和隐私政策');
    return;
  }
  
  try {
    const formEl = document.querySelector('.register-form') as any;
    if (!formEl) return;
    
    await formEl.validate(async (valid: boolean) => {
      if (valid) {
        const success = await userStore.register({
          username: registerForm.value.username,
          password: registerForm.value.password,
          email: registerForm.value.email,
          phone: registerForm.value.phone,
          isMerchant: false
        });
        
        if (success) {
          ElMessage.success('用户注册成功，请登录');
          router.push('/login');
        } else {
          ElMessage.error(userStore.error || '注册失败');
        }
      }
    });
  } catch (error: any) {
    ElMessage.error(error.message || '注册时发生错误');
  }
};
</script>

<style scoped>
.user-register-page {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f8f9fa;
  padding: 20px;
}

.register-container {
  display: flex;
  width: 1000px;
  min-height: 700px;
  background: white;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
}

.left-panel {
  flex: 1;
  position: relative;
  overflow: hidden;
}

.side-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(rgba(255, 107, 152, 0.7), rgba(255, 69, 123, 0.9));
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
  text-align: center;
  padding: 0 40px;
}

.promo-text h2 {
  font-size: 32px;
  font-weight: 700;
  margin-bottom: 16px;
}

.promo-text p {
  font-size: 18px;
  opacity: 0.9;
}

.right-panel {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.logo-container {
  display: flex;
  align-items: center;
  margin-bottom: 25px;
}

.logo {
  width: 36px;
  height: 36px;
}

.logo-text {
  font-size: 22px;
  font-weight: 600;
  margin-left: 10px;
  color: #34495e;
}

.form-header {
  margin-bottom: 25px;
}

.form-header h1 {
  font-size: 26px;
  font-weight: 700;
  color: #2c3e50;
  margin-bottom: 6px;
}

.form-header p {
  color: #7f8c8d;
  font-size: 15px;
}

.register-form {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.form-row {
  margin-bottom: 16px;
}

.term-service {
  margin: 16px 0 24px;
  font-size: 14px;
}

.term-service a {
  color: #ff6b98;
  text-decoration: none;
}

.term-service a:hover {
  text-decoration: underline;
}

.submit-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  border-radius: 5px;
  background-color: #ff6b98;
  border-color: #ff6b98;
  margin-bottom: 20px;
}

.submit-button:hover {
  background-color: #ff4f84;
  border-color: #ff4f84;
}

.form-footer {
  text-align: center;
  margin-top: auto;
  color: #7f8c8d;
}

.form-footer p {
  margin: 8px 0;
  font-size: 14px;
}

.login-link, .merchant-link a {
  color: #ff6b98;
  font-weight: 600;
  text-decoration: none;
}

.login-link:hover, .merchant-link a:hover {
  text-decoration: underline;
}

@media (max-width: 992px) {
  .register-container {
    flex-direction: column;
    width: 100%;
    max-width: 550px;
    height: auto;
  }
  
  .left-panel {
    height: 180px;
  }
  
  .right-panel {
    padding: 30px 20px;
  }
}

@media (max-width: 576px) {
  .register-container {
    box-shadow: none;
    background: transparent;
  }
  
  .left-panel {
    display: none;
  }
  
  .right-panel {
    background: white;
    border-radius: 10px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
  }
}
</style> 